using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIMenu : MonoBehaviour
{

    public GameObject panelMenu;
    public GameObject panelChoose;
    public GameObject panelContinue;  

    public GameObject panelLoad;
    public Slider load;
    public Text text;
    public Button Handover;

    public Button ctn;

    public UISelect select;

    public static bool inited;
    private void Awake()
    {
        if (inited == false)
        {
            StaticData.Init();
            GameData.Load();
            inited = true;
        }
        ctn.interactable = GameData.listRoles.Count > 0;    
    }
    // Start is called before the first frame update

    void Start()
    {
        panelLoad.SetActive(false);
        panelChoose.SetActive(false);
        Handover.interactable = false;
    }


    public void CreateNewGame()
    {
        select.New();
        panelChoose.SetActive(true);
        panelMenu.SetActive(false);
    }

    public void ContinueGame()
    {
        if (GameData.listRoles.Count > 1)
        {
            select.Read();
            panelChoose.SetActive(true);
            panelMenu.SetActive(false);
        }
        else EnterGame();
    }

    public void Quit()
    {
        Application.Quit();
    }

    public void EnterGame()
    {
        select.enabled = false;
        panelChoose.SetActive(false);
        panelLoad.SetActive(true);
        StartCoroutine(LoadScene());
    }

    IEnumerator LoadScene()
    {
        yield return null;
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("GameMain");
        asyncOperation.allowSceneActivation = false;
        while (!asyncOperation.isDone)
        {
            text.text = asyncOperation.progress * 100f + "%";
            load.value = 1f * asyncOperation.progress;
            Handover.interactable = asyncOperation.progress >= 0.9f;
            if (asyncOperation.progress >= 0.9f)
            {
                load.value = 1;
                text.text = 100 + "%";
                Handover.onClick.AddListener(() => asyncOperation.allowSceneActivation = true);
            }
            yield return null;
        }
    }


}


